package LinkedList.删除链表重复元素_一个不留;

import LinkedList.ListNode;

public class Test {

    public ListNode deleteDuplicates(ListNode head) {
        if(head == null || head.next == null) {
            return head;
        }

        ListNode dummy = new ListNode(-1);
        ListNode prev = dummy;
        ListNode cur = head;
        prev.next = head;

        while(cur != null) {
            while(cur.next != null && cur.val == cur.next.val) {
                cur = cur.next;
            }
            if(prev.next != cur) {
                prev.next = cur.next;
                cur = cur.next;
            }else{
                prev = cur;
                cur = cur.next;
            }
        }
        return dummy.next;
    }

}
